// arena
/*
 * Things to remember:
 * 1. Binary Search? DP? Greedy? Bitset?
 * 2. Find a conclusion?
 * 3. long long? Enough array size?
 * 4. Stay calm and focus on main points!
 * 5. CLEAR IN MULTI_TEST!!!
 * */
#include <cstdio>
#include <iostream>
#include <vector>
#include <set>
#define ll long long
#define rep(i, s, t) for(int i=s; i<=t; ++i)
#define debug(x) cerr<<#x<<":"<<x<<"\n";
using namespace std;
const int N=200010;

int T, n, nn, m, k, a[N], a1[N], b[N], c[N], cur[N];
int curn, curnn, curk;
vector<int> vec[N];
int simulate()
{
	int tmp=curnn/2;
	rep(i, 1, curnn) cur[i]=i;
	rep(r, 1, curk)
	{
		int cnt=0;
		rep(i, 1, tmp)
		{
			int x=cur[i*2-1], y=cur[i*2];
			if(vec[r][i]==0)
			{
				if(b[x]>=r) cur[++cnt]=x;
				else cur[++cnt]=y;
			}
			else
			{
				if(b[y]>=r) cur[++cnt]=y;
				else cur[++cnt]=x;
			}
		}
		tmp>>=1;
	}
	return cur[1];
}
int X[4];
void solve()
{
	rep(i, 0, 3) scanf("%d", X+i);
	rep(i, 1, n) a[i]=a1[i]^X[i%4];
	ll ans=0;
	rep(kk, 1, m)
	{
		curn=c[kk];
		curnn=1;
		for(curk=0; curnn<curn; curk++, curnn*=2) ;
		set<int> S;
		rep(i, 1, curnn)
		{
			if(i<=curn) b[i]=a[i];
			else b[i]=0;
		}
		S.insert(simulate());
		rep(i, curn+1, curnn)
		{
			b[i]=2147483647;
			S.insert(simulate());
			b[i]=0;
		}
		ll res=0;
		for(int x:S) res+=x;
		ans^=kk*res;
	}
	printf("%lld\n", ans);
}

int main()
{
	freopen("arena.in", "r", stdin);
	freopen("arena.out", "w", stdout);
	scanf("%d%d", &n, &m);
	rep(i, 1, n) scanf("%d", a1+i);
	rep(i, 1, m) scanf("%d", c+i);
	nn=1;
	for(k=0; nn<n; k++, nn*=2) ;
	int tmp=nn/2;
	rep(i, 1, k)
	{
		vec[i].resize(tmp+1);
		rep(j, 1, tmp) scanf("%1d", &vec[i][j]);
		tmp>>=1;
	}
	scanf("%d", &T);
	while(T--) solve();
	
	return 0;
}